<?php
/**
 * Timfan design模块处理程序
 *
 * @author Tim Fan
 * QQ:1026073477
 * @url http://i-fanr.com/
 */
defined('IN_IA') or exit('Access Denied');
define("TIM_QUIZ", "tim_quiz");
require_once IA_ROOT . "/addons/" . TIM_QUIZ . "/jssdk.class.php";
require_once IA_ROOT . "/addons/" . TIM_QUIZ . "/timUtil.class.php";
class Tim_quizModuleSite extends WeModuleSite {

	public function doMobileIndex() {
		
		include $this->template('index');
	}
	
	public function doMobileCover1() { 
		global $_W,$_GPC;
		$openid = $_W['openid'];
		$uniacid = $_W['uniacid'];
		$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tim_quiz')." WHERE uniacid=$uniacid" );
		$list = pdo_fetchall("SELECT * FROM ".tablename('tim_quiz')." WHERE uniacid=$uniacid ORDER BY qid ASC LIMIT 0,".($total-1));
		$all = pdo_fetchall("SELECT * FROM ".tablename('tim_quiz')." WHERE uniacid=$uniacid ORDER BY qid ASC");
		$last_one = pdo_fetchall("SELECT * FROM ".tablename('tim_quiz')." WHERE uniacid=$uniacid LIMIT ".($total-1).",".$total);
		$sets = pdo_fetch("SELECT * FROM ".tablename('tim_quiz_setting')." WHERE uniacid=$uniacid");
		$answer = array();
		foreach ($all as $value) {
			$page_num = "page-".$value['qid'];
			$answer[$page_num] = $this->_zh($value['correct_ans']);
		}
		$result=array(
			array(
				"weixin1"=>$sets['score_scope11'],
				"weixin2"=>$sets['score_scope12'],
				"praise"=>$sets['praise1'],
			),
			array(
				"weixin1"=>$sets['score_scope21'],
				"weixin2"=>$sets['score_scope22'],
				"praise"=>$sets['praise2'],
			),
			array(
				"weixin1"=>$sets['score_scope31'],
				"weixin2"=>$sets['score_scope32'],
				"praise"=>$sets['praise3'],
			),
			array(
				"weixin1"=>$sets['score_scope41'],
				"weixin2"=>$sets['score_scope42'],
				"praise"=>$sets['praise4'],
			),
			array(
				"weixin1"=>$sets['score_scope51'],
				"weixin2"=>$sets['score_scope52'],
				"praise"=>$sets['praise5'],
			)
		);
		
		include $this->template('index');
	}

	private function _zh($cans) { 
		switch ($cans)
		{
			case A:
			  return "0";
			  break;
			case B:
			  return "1";
			  break;
			case C:
			  return "2";
			  break;
			case D:
		      return "3";
			break;
			default:
			  return "Undefined";
		};
	}

	public function doWebTest() { 
		global $_W,$_GPC;
		$uniacid = $_W['uniacid'];
		$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		load()->func('tpl');
		if('post' == $op){//添加或修改
			$id = intval($_GPC['id']);
			if(!empty($id)){
			$item = pdo_fetch("SELECT * FROM ".tablename('tim_quiz')." where id=$id and uniacid=$uniacid");
			empty($item)?message('亲,数据不存在！', '', 'error'):"";	
			}
			$info = pdo_fetch("SELECT * FROM ".tablename('tim_quiz_setting')." where uniacid=$uniacid");
 			
			if(checksubmit('submit')){
				empty ($_GPC['question'])?message('亲,问题不能为空'):$question=$_GPC['question'];
				$type =$_GPC['type'];
				$qid  =$_GPC['qid'];
				$ans_a =$_GPC['ans_a'];
				$ans_b =$_GPC['ans_b'];
				$ans_c =$_GPC['ans_c'];
				$ans_d =$_GPC['ans_d'];
				$pic_a =$_GPC['pic_a'];
				$pic_b =$_GPC['pic_b'];
				$pic_c =$_GPC['pic_c'];
				$pic_d =$_GPC['pic_d'];
				$pic_a_intro =$_GPC['pic_a_intro'];
				$pic_b_intro =$_GPC['pic_b_intro'];
				$pic_c_intro =$_GPC['pic_c_intro'];
				$pic_d_intro =$_GPC['pic_d_intro'];
				$correct_ans =$_GPC['correct_ans'];
				$data = array(
					'id'=>$id,
					'uniacid' => $uniacid,
					'question' =>$question,
					'type' =>$type,
					'qid'  =>$qid,
					'ans_a'=>$ans_a,
					'ans_b'=>$ans_b,
					'ans_c'=>$ans_c,
					'ans_d'=>$ans_d,
					'pic_a'=>$pic_a,
					'pic_b'=>$pic_b,
					'pic_c'=>$pic_c,
					'pic_d'=>$pic_d,
					'pic_a_intro'=>$pic_a_intro,
					'pic_b_intro'=>$pic_b_intro,
					'pic_c_intro'=>$pic_c_intro,
					'pic_d_intro'=>$pic_d_intro,
					'correct_ans'=>$correct_ans,
				);
				
				if(empty($id)){
						pdo_insert('tim_quiz', $data);//添加数据
						message('数据添加成功！', $this->createWebUrl('test', array('op' => 'display')), 'success');
				}else{
						pdo_update('tim_quiz', $data, array('id' => $id));
						message('数据更新成功！', $this->createWebUrl('test', array('op' => 'display')), 'success');
				}
				
			}else{
				include $this->template('test');
			}
			
		}else if('del' == $op){//删除
		
			if(isset($_GPC['delete'])){
				$ids = implode(",",$_GPC['delete']);
				$sqls = "delete from  ".tablename('tim_quiz')."  where id in(".$ids.")"; 
				pdo_query($sqls);
				message('删除成功！', referer(), 'success');
			}
			$id = intval($_GPC['id']);
			$row = pdo_fetch("SELECT id FROM ".tablename('tim_quiz')." WHERE id = :id", array(':id' => $id));
			if (empty($row)) {
				//dump($_GPC);
				message('抱歉，数据不存在或是已经被删除！', $this->createWebUrl('test', array('op' => 'display')), 'error');
			}
			pdo_delete('tim_quiz', array('id' => $id));
			message('删除成功！', referer(), 'success');
			
		}else if('display' == $op){//显示
			$pindex = max(1, intval($_GPC['page']));
			$psize =20;//每页显示
			$condition = "WHERE uniacid= $uniacid";
			if (!empty($_GPC['keyword'])) {
				$condition .= " and  question LIKE '%".$_GPC['keyword']."%'  ";
			}
			
			$list = pdo_fetchall("SELECT *  FROM ".tablename('tim_quiz') ." $condition  ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize);//分页
			$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tim_quiz')." $condition" );
			$pager = pagination($total, $pindex, $psize);
			include $this->template('test');
		}
	}

	public function doWebParam() { 
		global $_W,$_GPC;
		$uniacid = $_W['uniacid'];
		load()->func('tpl');
		$info = pdo_fetch("SELECT * FROM ".tablename('tim_quiz_setting')." WHERE uniacid=$uniacid");
 			
		if(checksubmit('submit')){ 
				$title = $_GPC['title'];
				$copyright = $_GPC['copyright'];
				$score_scope11 = $_GPC['score_scope11'];
				$score_scope12 = $_GPC['score_scope12'];
				$praise1 = $_GPC['praise1'];
				$score_scope21 = $_GPC['score_scope21'];
				$score_scope22 = $_GPC['score_scope22'];
				$praise2 = $_GPC['praise2'];
				$score_scope31 = $_GPC['score_scope31'];
				$score_scope32 = $_GPC['score_scope32'];
				$praise3 = $_GPC['praise3'];
				$score_scope41 = $_GPC['score_scope41'];
				$score_scope42 = $_GPC['score_scope42'];
				$praise4 = $_GPC['praise4'];
				$score_scope51 = $_GPC['score_scope51'];
				$score_scope52 = $_GPC['score_scope52'];
				$praise5 = $_GPC['praise5'];
				$infos = array(
					'uniacid'=>$uniacid,
					'title' => $title, 
					'copyright' => $copyright,
					'score_scope11' => $score_scope11, 
					'score_scope12' => $score_scope12, 
					'praise1' => $praise1,
					'score_scope21' => $score_scope21, 
					'score_scope22' => $score_scope22, 
					'praise2' => $praise2,
					'score_scope31' => $score_scope31, 
					'score_scope32' => $score_scope32, 
					'praise3' => $praise3,
					'score_scope41' => $score_scope41, 
					'score_scope42' => $score_scope42, 
					'praise4' => $praise4,
					'score_scope51' => $score_scope51, 
					'score_scope52' => $score_scope52, 
					'praise5' => $praise5
				);
			if(empty($info)){
					pdo_insert('tim_quiz_setting', $infos);//添加数据
					message('数据添加成功！', $this->createWebUrl('param'), 'success');
			}else{
					$id = $info['id'];
					pdo_update('tim_quiz_setting', $infos, array('id' => $id));
					message('数据更新成功！', $this->createWebUrl('param'), 'success');
			}	
		}
		include $this->template('param');
	}


	
}